深入解析与实践Mockito:Java单元测试的强大助手 您所在的位置:网站首页 java process类模拟 深入解析与实践Mockito:Java单元测试的强大助手

深入解析与实践Mockito:Java单元测试的强大助手

2024-07-14 08:30| 来源: 网络整理| 查看: 265

一、Mockito概念 引言

Mockito是Java生态系统中最受欢迎的单元测试模拟框架之一,以其简洁易用的API和强大的模拟能力赢得了广大开发者的青睐。Mockito允许我们在不实际依赖外部资源的情况下对代码进行彻底且高效的单元测试,极大地提升了测试覆盖率和代码质量。

什么是Mockito

Mockito是一种模拟框架,其核心概念是在测试过程中创建并使用“Mock对象”。Mock对象是对实际对象的一种模拟,它继承或实现了被测试类所依赖的接口或类,但其行为可以根据测试需求自由定制。控制其在测试环境下的行为,从而将注意力聚焦于类本身的逻辑验证上。

Mockito的优势 隔离度高:通过模拟依赖,减少测试间的耦合,确保单元测试真正只关注被测试单元的内部逻辑。易于使用:API设计直观简洁,降低了编写和阅读测试用例的难度。详尽的验证:能够准确跟踪和验证被测试对象与其依赖之间的交互行为。灵活性强:支持多种定制模拟行为,无论是简单的返回值还是复杂的回调机制。有利于TDD实践:与测试驱动开发方法论紧密契合,鼓励写出更易于测试的代码。 二、Mockito的主要功能点和方法使用 Mock 方法 Mock对象创建

使用Mockito.mock()方法创建接口或抽象类的Mock对象。

public static T mock(Class classToMock) classToMock:待 mock 对象的 class 类。返回 mock 出来的类

实例:使用 mock 方法 mock 一个类

Random random = Mockito.mock(Random.class); Mock对象进行行为验证和结果断言 使用 verify 验证

验证是校验对象是否发生过某些行为,Mockito 中验证的方法是:verify。

verify(mock).someMethod("some arg"); verify(mock, times(1)).someMethod("some arg");

验证交换:Verify 配合 time() 方法,可以校验某些操作发生的次数。 注意:当使用 mock 对象时,如果不对其行为进行定义,则 mock 对象方法的返回值为返回类型的默认值。

/** * 测试Mockito框架的使用,模拟Random类的nextInt方法。 * 该测试函数没有参数和返回值,主要用于演示Mockito的基本用法。 */ @Test public void test01() { // 使用Mockito模拟一个Random对象 Random random = Mockito.mock(Random.class); // 调用nextInt()方法,输出随机数,因random 行为为进行打桩,故输出默认值0(random.nextInt() 返回的是int类型) System.out.println("第一次:"+random.nextInt()); // 指定当调用nextInt()时,始终返回1 Mockito.when(random.nextInt()).thenReturn(1); System.out.println("第二次:"+random.nextInt()); // 再次调用nextInt(),输出应为1 // 断言nextInt()方法返回值是否为1 Assertions.assertEquals(1,random.nextInt()); // 验证nextInt()方法是否被调用了两次 verify(random, times(3)).nextInt(); } 断言使用到的类是 Assertions Random random = Mockito.mock(Random.class); Assertions.assertEquals(2, random.nextInt());

输出结果:断言nextInt()方法返回值是否为1

org.opentest4j.AssertionFailedError: Expected :2 Actual :1 Mock对象打桩

打桩可以理解为mock对象规定一行的行为,使其按照我们的要求来执行具体的操作。在Mockito中,常用的打桩方法为

方法含义when().thenReturn()Mock 对象在触发指定行为后返回指定值when().thenThrow()Mock 对象在触发指定行为后抛出指定异常when().doCallRealMethod()Mock 对象在触发指定行为后调用真实的方法

thenReturn() 代码示例

@Test void check() { Random random = Mockito.mock(Random.class); Mockito.when(random.nextInt()).thenReturn(100); Assertions.assertEquals(100, random.nextInt()); } Mock 静态方法

首先要引入 Mockito-Inline 的依赖

org.mockito mockito-inline 4.3.1 test

使用 mockStatic() 方法来 mock静态方法的所属类,此方法返回一个具有作用域的模拟对象。

/** * 测试方法 test01,用于验证 StringUtils 类的 joinWith 方法的功能。 * 该方法模拟了静态方法 StringUtils.joinWith 的行为,以检查其是否能正确地将列表元素用指定分隔符连接成一个字符串。 */ @Test public void testJoinWith() { // 使用 Mockito 框架模拟 StringUtils 类的静态方法 MockedStatic stringUtilsMockedStatic = Mockito.mockStatic(StringUtils.class); // 创建一个字符串列表,作为 joinWith 方法的输入参数 List stringList = Arrays.asList("a", "b", "c"); // 配置模拟行为,当调用 StringUtils.joinWith(",", stringList) 时,返回 "a,b,c" stringUtilsMockedStatic.when(() -> StringUtils.joinWith(",", stringList)).thenReturn("a,b,c"); // 断言验证模拟行为是否正确,即 joinWith 方法返回的字符串是否与预期的 "a,b,c" 相等 Assertions.assertTrue(StringUtils.joinWith(",", stringList).equals("a,b,c")); } /** * 测试StringUtils类中的join方法。 * 该测试使用Mockito框架来模拟静态方法的行为,验证join方法是否按照预期工作。 * */ @Test public void testJoin() { // 使用Mockito模拟StringUtils类的静态方法 MockedStatic stringUtilsMockedStatic = Mockito.mockStatic(StringUtils.class); // 创建一个字符串列表作为join方法的输入 List stringList = Arrays.asList("a", "b", "c"); // 配置模拟行为,当调用StringUtils.join(",", stringList)时,返回字符串"a,b,c" stringUtilsMockedStatic.when(() -> StringUtils.join(",", stringList)).thenReturn("a,b,c"); // 断言验证模拟行为是否正确,即 join 方法返回的字符串是否与预期的 "a,b,c" 相等 Assertions.assertTrue(StringUtils.join(",", stringList).equals("a,b,c")); }

执行整个测试类后会报错:

org.mockito.exceptions.base.MockitoException: For com.echo.mockito.Util.StaticUtils, static mocking is already registered in the current thread To create a new mock, the existing static mock registration must be deregistered

原因是因为 mockStatic() 方法是将当前需要 mock 的类注册到本地线程上(ThreadLocal),而这个注册在一次 mock 使用完之后是不会消失的,需要我们手动的去销毁。如过没有销毁,再次 mock 这个类的时候 Mockito 将会提示我们 :”当前对象 mock 的对象已经在线程中注册了,请先撤销注册后再试“。这样做的目的也是为了保证模拟出来的对象之间是相互隔离的,保证同时和连续的测试不会收到上下文的影响。 因此我们修改代码:

public class MockitoStaticTest { /** * 测试方法 test01,用于验证 StringUtils 类的 joinWith 方法的功能。 * 该方法模拟了静态方法 StringUtils.joinWith 的行为,以检查其是否能正确地将列表元素用指定分隔符连接成一个字符串。 */ @Test public void testJoinWith() { // 使用 Mockito 框架模拟 StringUtils 类的静态方法 try (MockedStatic stringUtilsMockedStatic = Mockito.mockStatic(StringUtils.class)) { // 创建一个字符串列表,作为 joinWith 方法的输入参数 List stringList = Arrays.asList("a", "b", "c"); // 配置模拟行为,当调用 StringUtils.joinWith(",", stringList) 时,返回 "a,b,c" stringUtilsMockedStatic.when(() -> StringUtils.joinWith(",", stringList)).thenReturn("a,b,c"); // 断言验证模拟行为是否正确,即 joinWith 方法返回的字符串是否与预期的 "a,b,c" 相等 Assertions.assertTrue(StringUtils.joinWith(",", stringList).equals("a,b,c")); } } /** * 测试StringUtils类中的join方法。 * 该测试使用Mockito框架来模拟静态方法的行为,验证join方法是否按照预期工作。 */ @Test public void testJoin() { // 使用Mockito模拟StringUtils类的静态方法 try (MockedStatic stringUtilsMockedStatic = Mockito.mockStatic(StringUtils.class)) { // 创建一个字符串列表作为join方法的输入 List stringList = Arrays.asList("a", "b", "c"); // 配置模拟行为,当调用StringUtils.join(",", stringList)时,返回字符串"a,b,c" stringUtilsMockedStatic.when(() -> StringUtils.join(",", stringList)).thenReturn("a,b,c"); // 断言验证模拟行为是否正确,即 join 方法返回的字符串是否与预期的 "a,b,c" 相等 Assertions.assertTrue(StringUtils.join(",", stringList).equals("a,b,c")); } } } thenThrow 方法定义 /** * 测试当调用add方法时抛出RuntimeException异常的情况。 * 该测试函数不接受参数,也没有返回值。 */ @Test void testAddException() { // 设置mock对象,在调用mockitoTestController的add方法时抛出RuntimeException异常 when(mockitoTestController.add(1, 2)).thenThrow(new RuntimeException("add error")); // 验证是否抛出了RuntimeException异常 Assertions.assertThrows(RuntimeException.class, () -> mockitoTestController.add(1, 2)); } 三、Mockito 中常用注解 可以代替 Mock 方法的 @Mock 注解

Shorthand for mocks creation - @Mock annotation Important! This needs to be somewhere in the base class or a test runner:

快速 mock 的方法,使用 @mock 注解。 mock 注解需要搭配 MockitoAnnotations.openMocks(testClass) 方法一起使用。

@Mock private Random random; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); } /** * 测试Mockito框架的使用,模拟Random类的nextInt方法。 * 该测试函数没有参数和返回值,主要用于演示Mockito的基本用法。 */ @Test public void test02() { // 调用nextInt()方法,输出随机数,因random 行为为进行打桩,故输出默认值0(random.nextInt() 返回的是int类型) System.out.println("第一次:"+random.nextInt()); // 指定当调用nextInt()时,始终返回1 Mockito.when(random.nextInt()).thenReturn(1); System.out.println("第二次:"+random.nextInt()); // 再次调用nextInt(),输出应为1 // 断言nextInt()方法返回值是否为1 Assertions.assertEquals(1,random.nextInt()); // 验证nextInt()方法是否被调用了两次 verify(random, times(3)).nextInt(); } @BeforeEach 与 @BeforeAfter 注解 @Slf4j(topic = "RandomTest02") public class RandomTest02 { @Mock private Random random; @BeforeEach void setUp() { log.info("==============测试前准备==============="); MockitoAnnotations.openMocks(this); } /** * 测试Mockito框架的使用,模拟Random类的nextInt方法。 * 该测试函数没有参数和返回值,主要用于演示Mockito的基本用法。 */ @Test public void test02() { // 调用nextInt()方法,输出随机数,因random 行为为进行打桩,故输出默认值0(random.nextInt() 返回的是int类型) System.out.println("第一次:"+random.nextInt()); // 指定当调用nextInt()时,始终返回1 Mockito.when(random.nextInt()).thenReturn(1); System.out.println("第二次:"+random.nextInt()); // 再次调用nextInt(),输出应为1 // 断言nextInt()方法返回值是否为1 Assertions.assertEquals(1,random.nextInt()); // 验证nextInt()方法是否被调用了两次 verify(random, times(3)).nextInt(); } @AfterEach void tearDown() { log.info("==============测试后结果==============="); } }

而在 Junit5 中,@Before 和 @After 注解被 @BeforeEach 和 @AfterEach 所替代。

Spy 方法与 @Spy 注解

spy() 方法与 mock() 方法不同的是

被 spy 的对象会走真实的方法,而 mock 对象不会spy() 方法的参数是对象实例,mock 的参数是 class

示例:spy 方法与 Mock 方法的对比

/** * 测试方法,检查 Mockito 框架的使用。 * 无参数。 * 无返回值,但期望通过断言验证操作结果。 */ @Test void check() { // 调用实际的 mockitoTestController 对象的 add 方法,并验证结果是否为预期值 int result = mockitoTestController.add(1, 2); Assertions.assertEquals(3, result); // 使用 Mockito 创建 mockitoTest 的 mock 对象,并对它调用 add 方法,然后验证结果 MockitoTestController mockitoTest = Mockito.mock(MockitoTestController.class); int result1 = mockitoTest.add(1, 2); Assertions.assertEquals(3, result1); }

输出结果

// 第二个 Assertions 断言失败,因为没有给 mockitoTest 对象打桩,因此返回默认值 org.opentest4j.AssertionFailedError: Expected :3 Actual :0

使用 @Spy 注解代码示例

@Slf4j @ExtendWith(MockitoExtension.class) class MockitoTestControllerTest { @Spy private MockitoTestController mockitoTestController; @BeforeEach void setUp() { } /** * 测试add方法 * 该方法模拟调用mockitoTestController的add方法,传入参数1和2,期望返回值为3。 * 首先,通过when语句设置mockitoTestController的add方法返回值为3; * 然后,使用assertThat断言验证调用add方法(1, 2)实际返回值确实为3; * 最后,通过verify语句确认mockitoTestController的add方法确实被调用了一次,并传入了参数1和2。 */ @Test void testAdd() { // 设置mock对象的行为(打桩),当调用add(1, 2)时返回4 when(mockitoTestController.add(1, 2)).thenReturn(4); // 调用mock对象的方法,返回为4 int result = mockitoTestController.add(1, 2); log.info("mockitoTestController.add result={}",result); // 断言验证:调用add(1, 2)方法返回值是否为4 assertThat(mockitoTestController.add(1, 2)).isEqualTo(4); // 验证:确保add方法(1, 2)被调用了一次 verify(mockitoTestController,times(2)).add(1, 2); } } 四、Mockito使用总结

总结来说,Mockito通过模拟依赖、设置行为预期、验证交互和处理异常等方式,极大地增强了Java单元测试的可靠性和效率。无论是在小型项目还是大型企业级应用中,Mockito都是提升测试覆盖率和代码质量不可或缺的工具。

五、参考文档 Mockito 中文文档 ( 2.0.26 beta ) - 《Mockito 框架中文文档》 - 极客文档Spring Boot集成单元测试之如何mock_springboot mock测试-CSDN博客 六、往期推荐 六、往期推荐

1. Spring Boot 整合 Mockito:提升Java单元测试的高效实践 2. 通义灵码使用教程:探索AI编码的新维度 3. 流程编排是如何实现解耦代码

欢迎大家一键三连,如果发现文章中有错误或遗漏的地方,欢迎大家指正!


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有